package com.hezongxi.controller;
import com.hezongxi.entity.User;
import com.hezongxi.service.UserSevice;
import com.hezongxi.utils.JwtUtil;
import com.hezongxi.utils.VerifyCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
@CrossOrigin //允许跨域
@Slf4j
public class UserController {
    @GetMapping("yes")
    public void test(){
        System.out.println("success");
    }
    @Autowired()
    private UserSevice userSevice;
    public static String code;
    public String codeImg;
    @GetMapping("getImage")
    public String getImageCode(HttpServletRequest request) throws IOException {
       String code= VerifyCodeUtils.generateVerifyCode(4); //创建验证码
       request.getServletContext().setAttribute("code",code);//将验证码存在ServletContext中
       ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();//实例化字节输出流
       VerifyCodeUtils.outputImage(220,60,byteArrayOutputStream,code);//输出验证码
        this.code=(String) request.getServletContext().getAttribute("code");//将验证码存入成员变量
        codeImg="data:image/png;base64,"+Base64Utils.encodeToString(byteArrayOutputStream.toByteArray());
        System.out.println(this.code);//打印生成的验证码
       return "data:image/png;base64,"+Base64Utils.encodeToString(byteArrayOutputStream.toByteArray());//返回base64格式的验证码图片
    }



    @PostMapping("login")
    public Map<String,Object> login(@RequestBody User user,HttpServletRequest request) throws IOException {
        System.out.println("login");
        System.out.println(this.code.equalsIgnoreCase(user.getCode()));
        log.info("当前登录用户的信息：[{}]",user.toString());
        Map<String,Object> map = new HashMap<>();
        if (this.code.equalsIgnoreCase(user.getCode())) {
            try {
                User userDB = userSevice.login(user);
                userSevice.findAllUser();
                userSevice.login(user);
                map.put("msg", "登录成功");
                map.put("code", "200");
                System.out.println(JwtUtil.createToken(user.getUsername(),user.getPassword()));
                String token = JwtUtil.createToken(user.getUsername(),user.getPassword());
                map.put("token",token);
                System.out.println(user.getUsername()+user.getPassword());
            } catch (Exception e) {
                map.put("msg", "登录失败，用户名或密码错误");
                map.put("code", "201");
                getImageCode(request);
                map.put("codeImg",this.codeImg);
            }
        }else {
            map.put("msg", "验证码输入错误");
            map.put("code", "301");
            getImageCode(request);
            map.put("codeImg",this.codeImg);
        }
        return map;
    }
    @GetMapping("findAllUser")
    public List<User> findAllUser(){
        Map<String,Object> map=new HashMap();
        List<User> list = userSevice.findAllUser();
        return list;
    }
    @PostMapping("addUser")
    public Map<String,Object> addUser(@RequestBody User user,HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        try{
            userSevice.addUser(user);
            map.put("msg","添加成功！");
            map.put("state",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg","添加失败！");
            map.put("state",false);
        }
        return map;
     }
     @GetMapping("deleteUser")
        public Map<String,Object> delete(String id){
         Map<String,Object> map =new HashMap<>();
         try {
             log.info(id);
             userSevice.deleteUser(id);
             map.put("state",true);
             map.put("msg","删除员工信息成功！");
         }catch (Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","删除员工信息失败！");
         }


         return map;
     }


    }

